package com.cyz.service.impl;

import java.util.List;

import com.cyz.dao.UserDao;
import com.cyz.dao.impl.UserDaoImpl;
import com.cyz.exception.PasswordErrException;
import com.cyz.exception.UserNoExsitException;
import com.cyz.pojo.User;
import com.cyz.service.UserService;
import com.cyz.util.MD5Util;

public class UserServiceImpl implements UserService {

	private UserDao userDao = new UserDaoImpl();
	/**
	 * ��֤�û��˺ź����� �ɹ���½
	 */
	@Override
	public void validate(String username, String password) throws Exception {
		UserDao userDao = new UserDaoImpl();
		Integer userId = userDao.getUserIdByUserName(username); //�û�ID
		User user = userDao.getUserById(userId); //�û�����
		//System.out.println(user.getUserName());
		if(null == user.getUserName()) { //����û���������
			System.out.println(user.getUserName());
			throw new UserNoExsitException();
		} 
			if(!MD5Util.toMd5(password).equals(user.getPassword())) { //����������
				throw new PasswordErrException();
			}
		
	}
	/**
	 * �����û�
	 */

	@Override
	public void register(String userName, String password, String phone, String address)
			throws Exception {
		// TODO Auto-generated method stub
		
		UserDao userDao = new UserDaoImpl();
		userDao.register(userName, password, phone, address);
		
	
	}
//////////////////////////////////
	@Override
	public User getUserByName(String userName) {
		return userDao.getUserByName(userName);
	}
	@Override
	public Integer getUserIdByName(String userName) {
		return userDao.getUserIdByName(userName);
	}
	@Override
	public void addUser(User user) {
		userDao.addUser(user);
	}
	@Override
	public void updateUser(User user) {
		userDao.updateUser(user);
	}
	@Override
	public User findUserById(Integer userId) {
		return userDao.findUserById(userId);
	}
	@Override
	public Integer getUserIdByUserName(String userName) {
		UserDao userDao=new UserDaoImpl();
		return userDao.getUserIdByUserName(userName);
	}
	@Override
	public List<User> getAllUser() {
		return userDao.getAllUser();
	}
	@Override
	public void deleteUser(User user) {
		userDao.deleteUser(user);
	}
	@Override
	public void updateInfo(Integer id, String userName, String mobile, String address) {
		// TODO Auto-generated method stub
		UserDao userDao=new UserDaoImpl();
		userDao.updateInfo(id, userName, mobile, address);
		
	}
	@Override
	public void changePassword(Integer id, String newPassword) {
		// TODO Auto-generated method stub
		UserDao userDao=new UserDaoImpl();
		userDao.changePassword(id, newPassword);
		
	}
	
}
